#pragma once
#include "Noncopyable.h"
#include <unordered_map>
#include <vector>

class Channel;
class EventLoop;
class Timestamp;
// 多路事件分发器的核心IO复用模块
class Poller : Noncopyable
{
  public:
    using ChannelList = std::vector<Channel *>;
    Poller(EventLoop *loop);
    virtual ~Poller() = default;
    // 设定统一接口
    virtual Timestamp poll(int timeoutMs, ChannelList *activeChannels) = 0;
    virtual void updateChannel(Channel *) = 0;
    virtual void removeChannel(Channel *) = 0;
    // 判断channel在当前poller当中
    bool hasChannel(Channel *channel) const;

    // eventloop通过该接口获得一个默认的poller
    static Poller *newDefaultPoller(EventLoop *loop);

  protected:
    using ChannelMap = std::unordered_map<int, Channel *>;
    // key: sockfd value: sockfd所属的Channel
    ChannelMap channels_;

  private:
    EventLoop *ownerLoop_; // poller 所属的事件循环
};                         // class Poller
